<?php
/** 
 * Copyright (c) 2012, Jiehun.com.cn Inc. All Rights Reserved
 * @author Ronnie(dengxiaolong@jiehun.com.cn)
 * @date 2012-3-2
 * @version 1.0 
 * @brief 图片显示
 *  
 **/
class ZendViewPictureHelper extends Zend_View_Helper_Abstract
{
	private $image;
	private $host;
	private $sHost;
	private $fs;

	function __construct()
	{
		//$this->image = Com::get('image');
		$conf = Conf::get('hapn.host');
		$this->host = 'http://'.$conf['cdn'].'/';
		$this->sHost = 'http://'.$conf['static'].'/';
	}

	private function getFileSystem()
	{
		if (!$this->fs) {
			require_once EXLIB_ROOT.'/filesystem/FileSystem.php';
			$this->fs = new FileSystem(Conf::get('hapn.filesystem'));
		}
		return $this->fs;
	}
	
	function Picture($id, $ruleName = null, $lazyLoad = false)
	{
		if (!$id) {
			return $this->sHost.'/static/img/default/default.png';
		}
		if ($ruleName === null) {
			$ruleName = 'orig';
		}
		$fileId = $this->getFileSystem()->parseFileId($id);
		if ($ruleName != 'orig') {
			$rules = Conf::get('hapn.rules');
			if (!isset($rules[$ruleName])) {
				return '';
			}
			$rule = $rules[$ruleName];
			if (!$rule['width']) {
				$rule['width'] = intval($fileId['width'] * ($rule['height'] / $fileId['height']));
			} else if (!$rule['height']) {
				$rule['height'] = intval($fileId['height'] * ($rule['width'] / $fileId['width']));
			}
			return sprintf('%s%s/%s-%dx%d.%s', 
				$this->host, 
				$rule['type'],
				$fileId['fid'], 
				$rule['width'], 
				$rule['height'], 
				$fileId['ext']);
		}		
		return sprintf('%sorig/%s.%s', 
			$this->host, 
			$fileId['fid'], 
			$fileId['ext']);
	}
}